import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/utils/store.js';

//注册全局组件  router-link   router-view
Vue.use(VueRouter)


// 1. 定义 (路由) 组件。
// 可以从其他文件 import 进来
// @--src

// 2. 定义路由
const routes = [{
		name: 'Home',
		path: '/home',
		redirect: '/'
	},
	{
		name: 'Home',
		path: '/',
		component: () => import('@/views/Home.vue'),
		children: [
			{
				path: '/checkin',
				component:  () => import('@/views/customer_manage/checkin/index.vue'),
			},
			{
				path: '/customer',
				component:  () => import('@/views/customer_manage/customer/index.vue'),
			},
			{
				path: '/outgoing',
				component:  () => import('@/views/customer_manage/outgoing/index.vue'),
			},
			{
				path: '/retreat',
				component:  () => import('@/views/customer_manage/retreat/index.vue'),
			},
			{
				path: '/customerFood',
				component:  () => import('@/views/food_manage/customerFood/index.vue'),
			},
			{
				path: '/food',
				component:  () => import('@/views/food_manage/food/index.vue'),
			},{
				path: '/manager',
				component:  () => import('@/views/health_steward/manager/index.vue'),
			},{
				path: '/objservice',
				component:  () => import('@/views/health_steward/objservice/index.vue'),
			},
			{
				path: '/nurseContent',
				component:  () => import('@/views/nurse_manage/nurseContent/index.vue'),
			},
			{
				path: '/nurseLevel',
				component:  () => import('@/views/nurse_manage/nurseLevel/index.vue'),
			},{
				path: '/nurseRecording',
				component:  () => import('@/views/nurse_manage/nurseRecording/index.vue'),
			},
			{
				path: '/bed',
				component:  () => import('@/views/room_manage/bed/index.vue'),
			},
			{
				path: '/rooms',
				component:  () => import('@/views/room_manage/rooms/index.vue'),
			},
			{
				path: '/constantType',
				component:  () => import('@/views/constant/constantType/index.vue'),
			},
			{
				path: '/constantsItem',
				component:  () => import('@/views/constant/constantsItem/index.vue'),
			},
		]
	},
	{
		name: 'Login',
		path: '/login',
		component: () => import('@/views/Login.vue')
	},
	{
		path: '*',
		component: () => import('@/views/NotFound.vue')
	},



]

// 3. 创建 router 实例，然后传 `routes` 配置
// 你还可以传别的配置参数, 不过先这么简单着吧。

/**   
 * 
 
     Hash 
 *    http://127.0.0.1/#/user
 *    http://127.0.0.1/#/role
 *    http://127.0.0.1/#menu
 
     History
 *    http://127.0.0.1/user
 *    http://127.0.0.1/role
 *    http://127.0.0.1/menu
    
	
	VueRouter   --- Vue实例（根）
 */
const router = new VueRouter({
	// url 、
	// mode: 'history',
	mode: 'hash',
	routes // (缩写) 相当于 routes: routes
})


//全局的
router.beforeEach((to, from, next) => {
	// console.log("from",from);
	// console.log("to",to);
	// console.log("next",next);

	let toPath = to.path;
	if (toPath === '/login') {
		next()
	} else {   //     /
		//判断是否存在登录状态

		const token = store.state.token;
		if (token) {
			//可以放行
			next();
		} else {
			//没有登录去登录
			next("/login");
		}


	}

})


export default router